<?php defined( '_VALID_MOS' ) or die( include("404.php") );

class Pager
{ 
	function getPagerData($numHits, $limit, $page){ 
		//kiem tra du lieu nhap
		$numHits = (int) $numHits; //tong so mau tin
		$limit = max((int) $limit, 1); //so mau tin moi trang
		$page = (int) $page; //so trang dang duyet
		$numPages = ceil($numHits / $limit); // tinh tong so trang
		$page = max($page, 1); //neu page < 1 thì page = 1
		$page = min($page, $numPages); //neu page>$numPages thì page=$numPages
		$offset = ($page - 1) * $limit; 
		
		if ($_GET["view"] == "category"){
			$selfUrl = "./com_content/category/".$_GET["alias"]."/".$_GET["item"];
		} else if($_GET["view"] == "article"){
			$selfUrl = "./com_content/article/".$_GET["alias"]."/".$_GET["item"];
		}
		
		$maxP = 2;
		$revpage = $page - 1;
		$nexpage = $page + 1;
		if($page == 1 || $page == 0) {
			$paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/FirstDisabled.jpg\"> </a></li>";
			$paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/PreviousDisabled.jpg\"> </a></li>";
		}
		else {
			$paging = $paging ."<li class=\"page_text\"><a href=\"$selfUrl/1.csp\" title=\"Trang đầu\"><img src=\"templates/default/images/First.jpg\"></a></li>";
			$paging = $paging ."<li class=\"page_text\"><a href=\"$selfUrl/$revpage.csp\" title=\"Trang kế\"><img src=\"templates/default/images/Previous.jpg\"></a></li>";			
		}
		
		$page = $page;
		$pages = $numPages;
		$hien_tai = $page;
		$bien_trai = $hien_tai - $maxP;
		$bien_phai = $hien_tai + $maxP;

		if ($bien_trai<1) $bien_trai = 1;

		if ($bien_phai>$pages) $bien_phai = $pages;

		if ($bien_trai>1) $paging = $paging."<li class=\"page_img\"><a href=\"$selfUrl/1.csp\">1 </a></li>";

		if ($bien_trai>2) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

		for($i=$bien_trai; $i<=$bien_phai; $i++){
			if ($i==$hien_tai)
				$paging = $paging."<li class=\"page_active\"><a href=\"javascript:void(0)\">$i </a></li>";
			else
				$paging = $paging."<li class=\"page_img\"><a href=\"$selfUrl/$i.csp\">$i </a></li>";
		}

		if($bien_phai<$pages) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

		if($bien_phai<$pages - 1) $paging = $paging . "<li class=\"page_img\"><a href=\"$selfUrl/$pages.csp\">$pages </a></li>";

		if($page == $pages) {
			$paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/NextDisabled.jpg\"></a></li>";
			$paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/LastDisabled.jpg\"></a></li>";
		} else {
			$paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$nexpage.csp\" title=\"Trang sau\"><img src=\"templates/default/images/Next.jpg\"></a></li>";
			$paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$pages.csp\" title=\"Trang cuối\"><img src=\"templates/default/images/Last.jpg\"></a></li>";
		}
		
		$ret = new stdClass; 
		$ret->offset = $offset; 
		$ret->limit = $limit + $offset; 
		$ret->numPages = $numPages; 
		$ret->page = $page; 
		$ret->paging = $paging; 
		return $ret;
	} 

} 

?> 